package com.nc.delivery_project.server.dao;

import com.nc.delivery_project.server.domain.Department;
import com.nc.delivery_project.server.domain.OrderWaypoint;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;


/**
 * Created with IntelliJ IDEA.
 * User: Natalie D.
 * Date: 2/1/15
 * Time: 4:26 PM
 */
public class OrderWaypointDao extends AbstractDao<OrderWaypoint>
{

    protected final static String ENTITY_NAME = OrderWaypoint.class.getSimpleName();


    public OrderWaypointDao() {
        super(ENTITY_NAME);
    }
    public OrderWaypointDao(Connection connection) throws SQLException {
        super(connection, ENTITY_NAME);
    }

    @Override
    protected void prepareInsertStatement(CallableStatement statement, OrderWaypoint object) throws SQLException {
      //  statement.setInt(1, object.getId());
        statement.registerOutParameter(1, Types.NUMERIC);
        statement.setInt(2, object.getPriority());
        statement.setString(3, object.getStatus());
        statement.setDate(4, new java.sql.Date(object.getSentDate().getTime()));
        statement.setDate(5, new java.sql.Date(object.getReceivedDate().getTime()));
        statement.setInt(6, object.getDepartment().getDeptId());
    }

    @Override
    protected void prepareUpdateStatement(CallableStatement statement, OrderWaypoint object) throws SQLException {

    }


    @Override
    protected List<OrderWaypoint> parseResultSet(ResultSet rs) throws SQLException {
        List<OrderWaypoint> orderwaypoints = new ArrayList<>();
        while (rs.next())
        {
            int id=rs.getInt("OW_ID");

            Department dept = new Department(rs.getInt("DEPT"));
            int priority = rs.getInt("PRIORITY");
            Date sent=rs.getDate("SENT_DATE");
            Date received=rs.getDate("RECEIVED_DATE");
          //  String status = rs.getString("ORDER_WAYPOINT_STATUS");

            orderwaypoints.add(new OrderWaypoint(id,priority,"not defined",sent,received,dept));
        }

        return orderwaypoints;
    }

    protected String getReadByOrderIdQuery() {
        return queryManager.getQuery(ENTITY_NAME, "select_by_order_id");
    }

    protected String getReadByDepartmentIdQuery() {
        return queryManager.getQuery(ENTITY_NAME, "select_by_department_id");
    }

    public List<OrderWaypoint> readByOrderId(int id)
    {
        try
        {
            PreparedStatement statement = connection.prepareStatement(getReadByOrderIdQuery());
            statement.setInt(1, id);
            ResultSet rs = statement.executeQuery();
            return parseResultSet(rs);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public List<OrderWaypoint> readByDepartmentId(int id)
    {
        try
        {
            PreparedStatement statement = connection.prepareStatement(getReadByDepartmentIdQuery());
            statement.setInt(1, id);
            ResultSet rs = statement.executeQuery();
            return parseResultSet(rs);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public int create(OrderWaypoint obj) throws SQLException {
        return super.create(obj, 1);
    }
}
